package cn.doitedu.dp.controller;

import cn.doitedu.dp.pojo.LogHdfsDqBean;
import cn.doitedu.dp.pojo.LogServerDqBean;
import cn.doitedu.dp.pojo.LogServerVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class LogCollectionDqController {
    Connection connection;
    public LogCollectionDqController() throws SQLException {
        connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit47","root","root");
    }



    @RequestMapping("/dq/log_server")
    @ResponseBody
    public String reportLogServerDq( @RequestBody LogServerDqBean logServerDqBean){

        try {
            String sql = "insert into log_server_dq(file_cnt,line_amt,target_dt,run_dt,log_server) values(?,?,?,?,?)";
            java.sql.PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,logServerDqBean.getFile_cnt());
            ps.setInt(2,logServerDqBean.getLine_amt());
            ps.setString(3,logServerDqBean.getTarget_dt());
            ps.setString(4,logServerDqBean.getRun_dt());
            ps.setString(5,logServerDqBean.getLog_server());


            ps.execute();



        } catch (SQLException e) {
            return "fail";
        }

        return "success";
    }



    @RequestMapping("/dq/log_hdfs")
    @ResponseBody
    public String reportHdfsLogDq(@RequestBody  LogHdfsDqBean logHdfsDqBean){

        try {
            String sql = "insert into log_hdfs_dq(table_name,target_dt,run_dt,line_cnt) values(?,?,?,?)";

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,logHdfsDqBean.getTable_name());
            ps.setString(2,logHdfsDqBean.getTarget_dt());
            ps.setString(3,logHdfsDqBean.getRun_dt());
            ps.setInt(4,logHdfsDqBean.getLine_cnt());

            ps.execute();

        }catch (Exception e){
            return "fail";
        }

        return "success";
    }



    @RequestMapping("/dq/query/log_server")
    @ResponseBody
    public List<LogServerVo> queryLogServerDq(String queryDate ){

        System.out.println("接收到参数: " + queryDate);


        String sql = "select * from log_server_dq where target_dt = ?";

        ArrayList<LogServerVo> logServerVos = new ArrayList<>();

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,queryDate);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                LogServerVo logServerVo = new LogServerVo();

                logServerVo.setServer_name(resultSet.getString("log_server"));
                logServerVo.setFile_count(resultSet.getInt("file_cnt"));
                logServerVo.setLine_count(resultSet.getInt("line_amt"));
                logServerVo.setTarget_date(resultSet.getString("target_dt"));
                logServerVo.setRun_date(resultSet.getString("run_dt"));


                logServerVos.add(logServerVo);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return logServerVos;
    }


}
